﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess
{
    public class ScheduleDetailDetailDAL : BaseObject
    {
        #region Public Methods
        ///<summary>
        ///Create Obj from ireader.
        ///</summary>

        private ScheduleDetail GetObjectFromReader(IDataReader reader)
        {
            ScheduleDetail temp = new ScheduleDetail();
            temp.ScheduleID = (reader["ScheduleID"] is DBNull) ? int.MinValue : (System.Int32)reader["ScheduleID"];
            temp.FromStation = (reader["FromStation"] is DBNull) ? int.MinValue : (System.Int32)reader["FromStation"];
            temp.ToStation = (reader["ToStation"] is DBNull) ? int.MinValue : (System.Int32)reader["ToStation"];
            temp.FromTime = (reader["FromTime"] is DBNull) ? DateTime.MinValue : (System.DateTime)reader["FromTime"];
            temp.ToTime = (reader["ToTime"] is DBNull) ? DateTime.MinValue : (System.DateTime)reader["ToTime"];
            return temp;
        }

        ///<summary>
        ///Get List ScheduleDetail and store in a list 
        ///</summary>
        public ScheduleDetail Get_ScheduleDetail_By_ID(int ScheduleID, int FromStation, int ToStation)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", ScheduleID));
                cmd.Parameters.Add(new SqlParameter("@FromStation", FromStation));
                cmd.Parameters.Add(new SqlParameter("@ToStation", ToStation));
                conn.Open();
                ScheduleDetail customer = new ScheduleDetail();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                    {
                        customer = GetObjectFromReader(reader);
                    }
                }
                conn.Close();
                return customer;
            }
        }

        ///<summary>
        ///Get List ScheduleDetail and store in a list
        ///</summary>
        /// <param name="whereSql">Where fields. example : 1=1 </param>
        /// <param name="orderBy"Order fields></param>
        public List<ScheduleDetail> GetList_ScheduleDetail(string whereSql, string orderBy)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@WhereCondition", whereSql));
                cmd.Parameters.Add(new SqlParameter("@OrderByExpression", orderBy));
                conn.Open();
                List<ScheduleDetail> lst = new List<ScheduleDetail>();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        lst.Add(GetObjectFromReader(reader));
                    }
                }
                conn.Close();
                return lst;
            }
        }

        ///<summary>
        ///Get All ScheduleDetail and store in a list
        ///</summary>
        /// <param name="whereSql">Where fields. example : 1=1 </param>
        /// <param name="orderBy"Order fields></param>
        public List<ScheduleDetail> GetAll_ScheduleDetail()
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                List<ScheduleDetail> lst = new List<ScheduleDetail>();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        lst.Add(GetObjectFromReader(reader));
                    }
                }
                conn.Close();
                return lst;
            }
        }

        ///<summary>
        ///Using for paging data 
        ///</summary>
        /// <param name="pageIndex">Number of pages</param>
        /// <param name="PageSize">Number of a rows per page</param>
        /// <param name="conditions">Where  fields</param>
        /// <param name="groupBy">Group by fields</param>
        public DataTable Get_ScheduleDetailAsPaging(int pageIndex, int pageSize, string sSortColumn, string sFields, string sWhere, string sGroupBy)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                cmd.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                cmd.Parameters.Add(new SqlParameter("@SortColumn", sSortColumn));
                cmd.Parameters.Add(new SqlParameter("@Fields", sFields));
                cmd.Parameters.Add(new SqlParameter("@Where", sWhere));
                cmd.Parameters.Add(new SqlParameter("@Group", sGroupBy));
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    DataTable tbl = new DataTable();
                    dataAdapter.Fill(tbl);
                    conn.Close();
                    return tbl;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Insert new ScheduleDetail
        ///</summary>
        public void Insert_ScheduleDetail(ScheduleDetail temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", temp.ScheduleID));
                cmd.Parameters.Add(new SqlParameter("@FromStation", temp.FromStation));
                cmd.Parameters.Add(new SqlParameter("@ToStation", temp.ToStation));
                cmd.Parameters.Add(new SqlParameter("@FromTime", temp.FromTime));
                cmd.Parameters.Add(new SqlParameter("@ToTime", temp.ToTime));

                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Update a ScheduleDetail
        ///</summary>
        public void Update_ScheduleDetail(ScheduleDetail temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", temp.ScheduleID));
                cmd.Parameters.Add(new SqlParameter("@FromStation", temp.FromStation));
                cmd.Parameters.Add(new SqlParameter("@ToStation", temp.ToStation));
                cmd.Parameters.Add(new SqlParameter("@FromTime", temp.FromTime));
                cmd.Parameters.Add(new SqlParameter("@ToTime", temp.ToTime));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Delete a ScheduleDetail
        ///</summary>
        public void Delete_ScheduleDetail(ScheduleDetail temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", temp.ScheduleID));
                cmd.Parameters.Add(new SqlParameter("@FromStation", temp.FromStation));
                cmd.Parameters.Add(new SqlParameter("@ToStation", temp.ToStation));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        #endregion
    }
}
